#include "Operation.hpp"

using namespace cv;

Rect rect;
Mat src, roiImg, result;
Mat glbImg, wsgloimg,imgmask,imgmaskshow;
Point prePoint;
OCVLiveWire lw;
cv::Size labelsz;
std::vector<cv::Point> LWPath;


void LWMouse(int event, int x, int y, int flags, void*)
{
    bool isKeyShift = (flags & cv::EVENT_FLAG_SHIFTKEY);
    bool isKeyCtrl = (flags & cv::EVENT_FLAG_CTRLKEY);

    int interial_x = 0;
    int interial_y = 0;

    if (event == cv::EVENT_LBUTTONDOWN)
    {
        lw.calcLWP(cv::Point(x - interial_x, y), !isKeyShift);
        lw.incPath();
        
        //lw.helpDrawImgPXY();
    }
    else if (event == cv::EVENT_RBUTTONDOWN)
    {
        for (int ii = 1; ii < lw.pathTot.size(); ii++)
        {
            LWPath.push_back(lw.pathTot[ii - 1]);
        }
        cv::setMouseCallback("ps", NULL, NULL);
        cv::destroyWindow("ps");
    }
    else
    {
        cv::Point pxy(x - interial_x, y);
        if (lw.isStartedPath())
        {
            lw.calcLWPath(pxy, !isKeyShift);
            cv::Mat imgOUT;
            glbImg.copyTo(imgOUT);
            for (int ii = 1; ii < lw.pathTot.size(); ii++)
            {
                cv::line(imgOUT, lw.pathTot[ii - 1], lw.pathTot[ii], cv::Scalar(255, 0, 0), 2);
            }
            std::vector<cv::Point> tpath;
            tpath.push_back(lw.getCPoint());
            tpath.insert(tpath.end(), lw.path.begin(), lw.path.end());
            for (int ii = 1; ii < tpath.size(); ii++)
            {
                cv::line(imgOUT, tpath[ii - 1], tpath[ii], cv::Scalar(0, 255, 0), 2);
            }
            cv::circle(imgOUT, lw.getCPoint(), 3, cv::Scalar(0, 0, 255), 1);
            cv::imshow("win", imgOUT);
        }
    }
}

void myMouse(int event, int x, int y, int flags, void*)
{
    if (event == EVENT_LBUTTONDOWN)
    {
        prePoint = Point(x, y);
        cout << "轨迹起始坐标" << prePoint << endl;
    }
    if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
    {
        Point pt(x, y);
        line(glbImg, prePoint, pt, Scalar(0, 0, 255), 2, 5, 0);
        imshow("ps", glbImg);
        prePoint = pt;
    }

}

void WaterShedMouse(int event, int x, int y, int flags, void*)
{
    bool isKeyShift = (flags & cv::EVENT_FLAG_SHIFTKEY);
    bool isKeyCtrl = (flags & cv::EVENT_FLAG_CTRLKEY);

    int interial_x = 0;
    int interial_y = 0;

    
    if (event == EVENT_LBUTTONDOWN)
    {
        prePoint = Point(x - interial_x, y);

    }

    if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)) {
        Point pt(x - interial_x, y);
        line(imgmask, prePoint, pt, Scalar(0, 0, 255), 2, 5, 0);
        line(imgmaskshow, prePoint, pt, Scalar(0, 0, 255), 2, 5, 0);
        prePoint = pt;
        imshow("mask", imgmaskshow);
    }
    if (event == cv::EVENT_RBUTTONDOWN)
    {
        //for (int ii = 1; ii < lw.pathTot.size(); ii++)
        //{
        //    LWPath.push_back(lw.pathTot[ii - 1]);
        //}
        cv::setMouseCallback("ps", NULL, NULL);
        cv::destroyWindow("ps");
    }
}
